local qiuxian = fk.CreateSkill{
  name = "hanqing__qiuxian",
}

Fk:loadTranslationTable{
  ["hanqing__qiuxian"] = "求贤",
  [":hanqing__qiuxian"] = "游戏开始时，你声明三种锦囊牌名。中央区包含声明牌的回合结束时，你可以令一名角色获得其中一张牌。若为声明牌，"..
  "你再声明另一种锦囊牌名覆盖之；否则，你须重铸所有牌，或两张红色牌，或一张装备牌。",

  ["@$hanqing__qiuxian"] = "求贤",
  ["#hanqing__qiuxian-choice"] = "求贤：请选择%arg种牌名",
  ["#hanqing__qiuxian-choose"] = "求贤：你可以令一名角色获得中央区一张牌",
  ["#hanqing__qiuxian-prey"] = "求贤：获得中央区一张牌",
  ["#hanqing__qiuxian-replace"] = "求贤：请选择一种牌名替换【%arg】记录",
  ["#hanqing__qiuxian-recast"] = "求贤：请重铸符合条件的牌",
}

local U = require "packages/utility/utility"

qiuxian:addEffect(fk.GameStart, {
  anim_type = "support",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(qiuxian.name)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local all_choices = Fk:getAllCardNames("td", true)
    local n = math.min(#all_choices, 3)
    local mark = U.askForChooseCardNames(room, player, all_choices, n, n, qiuxian.name, "#hanqing__qiuxian-choice:::"..n)
    room:setPlayerMark(player, "@$hanqing__qiuxian", mark)
  end,
})

qiuxian:addEffect(fk.TurnEnd, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(qiuxian.name) and
      table.find(player.room:getBanner("@$CenterArea"), function (id)
        return table.contains(player:getTableMark("@$hanqing__qiuxian"), Fk:getCardById(id).trueName)
      end)
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local to = room:askToChoosePlayers(player, {
      min_num = 1,
      max_num = 1,
      targets = room.alive_players,
      skill_name = qiuxian.name,
      prompt = "#hanqing__qiuxian-choose",
      cancelable = true,
    })
    if #to > 0 then
      event:setCostData(self, {tos = to})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    local all_choices = Fk:getAllCardNames("td", true)
    local card = room:askToChooseCard(to, {
      target = target,
      flag = { card_data = {{ "@$CenterArea", room:getBanner("@$CenterArea") }} },
      skill_name = qiuxian.name,
      prompt = "#hanqing__qiuxian-prey",
    })
    local name = Fk:getCardById(card).trueName
    room:moveCardTo(card, Card.PlayerHand, to, fk.ReasonJustMove, qiuxian.name, nil, true, to)
    if player.dead then return end
    if table.contains(player:getTableMark("@$hanqing__qiuxian"), name) then
      local mark = player:getTableMark("@$hanqing__qiuxian")
      local choices = table.simpleClone(all_choices)
      for _, s in ipairs(mark) do
        table.removeOne(choices, s)
      end
      if #choices == 0 then return end
      local new_name = U.askForChooseCardNames(room, player, choices, 1, 1, qiuxian.name,
        "#hanqing__qiuxian-replace:::"..name, all_choices)[1]
      for i = 1, #mark, 1 do
        if mark[i] == name then
          mark[i] = new_name
          break
        end
      end
      room:setPlayerMark(player, "@$hanqing__qiuxian", mark)
    elseif not player:isNude() then
      local success, dat = room:askToUseActiveSkill(player, {
        skill_name = "#hanqing__qiuxian_active",
        prompt = "#hanqing__qiuxian-recast",
        cancelable = false
      })
      if not (success and dat) then
        dat = {}
        dat.cards = player:getCardIds("he")
      end
      if #dat.cards == 0 then
        dat.cards = player:getCardIds("he")
      end
      room:recastCard(dat.cards, player, qiuxian.name)
    end
  end,
})

qiuxian:addAcquireEffect(function (self, player, is_start)
  player.room:addSkill("#CenterArea")
end)

return qiuxian
